package com.shangcheng.dubbo.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shangcheng.dubbo.mappers.SmsCouponProductRelationMapper;
import com.shangcheng.model.domain.SmsCouponProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsCouponProductRelationApiImpl implements SmsCouponProductRelationApi {
    @Autowired
    private SmsCouponProductRelationMapper couponProductRelationMapper;

    @Override
    public void create(Long couponId, List<SmsCouponProductRelation> productRelationList) {
        for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
            smsCouponProductRelation.setCouponId(couponId);
            couponProductRelationMapper.insert(smsCouponProductRelation);
        }
    }

    @Override
    public List<SmsCouponProductRelation> findById(Integer couponId) {
        return couponProductRelationMapper.selectList(new LambdaQueryWrapper<SmsCouponProductRelation>().eq(SmsCouponProductRelation::getCouponId, couponId));
    }

    @Override
    public void updateByCouponId(Integer couponId, List<SmsCouponProductRelation> productRelationList) {
        this.deleteByCouponId(couponId);
        if (ObjectUtil.isNotEmpty(productRelationList)) {
            for (SmsCouponProductRelation couponProductRelation : productRelationList) {
                couponProductRelation.setCouponId(Convert.toLong(couponId));
                couponProductRelationMapper.insert(couponProductRelation);
            }
        }
    }

    @Override
    public void deleteByCouponId(Integer couponId) {
        couponProductRelationMapper.delete(new LambdaQueryWrapper<SmsCouponProductRelation>().eq(SmsCouponProductRelation::getCouponId, couponId));
    }
}
